home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / MacApp Release 10 / MacApp Release 10 - HD Ready / Examples / IconEdit / UIconEdit.cp < prev    next >
Encoding:
Text File  |  1996-04-03  |  68.7 KB  |  2,163 lines  |  [TEXT/MPS ]

  1. //----------------------------------------------------------------------------------------
  2. // UIconEdit.cp
  3. // copyright © 1988-96 by Apple Computer, Inc. All rights reserved.
  4. //----------------------------------------------------------------------------------------
  5.  
  6. #ifndef __UICONEDIT__
  7. #include "UIconEdit.h"
  8. #endif
  9.  
  10. // MacApp
  11.  
  12. #ifndef __TOOLBOX__
  13. #include "Toolbox.h"
  14. #endif
  15.  
  16. #ifndef __UCLIPBOARDMGR__
  17. #include "UClipboardMgr.h"
  18. #endif
  19.  
  20. #ifndef __UMACAPPGLOBALS__
  21. #include "UMacAppGlobals.h"
  22. #endif
  23.  
  24. #ifndef __UMACAPPUTILITIES__
  25. #include "UMacAppUtilities.h"
  26. #endif
  27.  
  28. #ifndef __UMEMORY__
  29. #include "UMemory.h"
  30. #endif
  31.  
  32. #ifndef __UMENUMGR__
  33. #include "UMenuMgr.h"
  34. #endif
  35.  
  36. #ifndef __UPRINTING__
  37. #include "UPrinting.h"
  38. #endif
  39.  
  40. #ifndef __USCRIPTING__
  41. #include "UScripting.h"
  42. #endif
  43.  
  44. #ifndef __USTREAM__
  45. #include "UStream.h"
  46. #endif
  47.  
  48. #ifndef __UVIEWSERVER__
  49. #include "UViewServer.h"
  50. #endif
  51.  
  52. #ifndef __UWINDOW__
  53. #include "UWindow.h"
  54. #endif
  55.  
  56. // Toolbox
  57.  
  58. #ifndef __AEREGISTRY__
  59. #include <AERegistry.h>
  60. #endif
  61.  
  62. #ifndef __ICONS__
  63. #include <Icons.h>
  64. #endif
  65.  
  66. #ifndef __PICKER__
  67. #include <Picker.h>
  68. #endif
  69.  
  70. #ifndef __TOOLUTILS__
  71. #include <ToolUtils.h>
  72. #endif
  73.  
  74. // ANSI
  75.  
  76. #ifndef __STDIO__
  77. #include <stdio.h>
  78. #endif
  79.  
  80. #ifndef __STDLIB__
  81. #include <stdlib.h>
  82. #endif
  83.  
  84. //----------------------------------------------------------------------------------------
  85. // Constants
  86.  
  87. const short kIconHBits = 32;                    // Number of horizontal bits in a bitmap.
  88. const short kIconVBits = 32;                    // Number of vertical bits in a bitmap.
  89.  
  90. const short kIconSizeInBytes = 128;                // Number of bytes in an bitmap.
  91. const short kIconSizeInLongs = 32;                // Number of long words in a bitmap.
  92.  
  93. // Resource identifiers
  94.  
  95. const short kSeedIconId = 1000;                    // Id of the seed icon resource.
  96. const short kIconWindowId = 1000;                // Id of the icon window 'view' resource.
  97. const short kIconEditViewId = 1001;                // Id of the TIconEditView resource.
  98. const short kPencilCursorId = 1000;                // Id of the pencil cursor resource.
  99.  
  100. // Constants for TIconEditView
  101.  
  102. const short kDefaultMagnification = 7;            // Default icon magnification.
  103. const short kBorder = 5;                        // Border in which to inset drawing.
  104. const ResType kIconClipType = 'ICON';            // Our private clipboard data type.
  105.  
  106. // Command Numbers
  107.  
  108. const CommandNumber cZoomIn = 1000;                // Zoom In menu command.
  109. const CommandNumber cZoomOut = 1001;            // Zoom Out menu command.
  110. const CommandNumber cInvert = 1002;                // Invert menu command.
  111. const CommandNumber cDrawCommand = 1003;        // For drawing in the icon edit view.
  112. const CommandNumber cDrawPointsCommand = 1004;    // set point apple event
  113. const CommandNumber cSetColor = 1005;            // set a new document color
  114.  
  115.  
  116.  
  117. //========================================================================================
  118. // CLASS TIconEditApplication
  119. //========================================================================================
  120. #undef Inherited
  121. #define Inherited TApplication
  122.  
  123. #pragma segment AInit
  124. MA_DEFINE_CLASS_M1(TIconEditApplication,
  125.                    Inherited);
  126.  
  127. //----------------------------------------------------------------------------------------
  128. // TIconEditApplication::IIconEditApplication: 
  129. //----------------------------------------------------------------------------------------
  130. #pragma segment AInit
  131.  
  132. void TIconEditApplication::IIconEditApplication()
  133. {
  134.     IApplication(kFileType, kSignature);
  135.  
  136.     MA_REGISTER_CLASS(TIconEditView);
  137. }
  138.  
  139. //----------------------------------------------------------------------------------------
  140. // TIconEditApplication destructor
  141. //----------------------------------------------------------------------------------------
  142. #pragma segment MADestructorRes
  143.  
  144. TIconEditApplication::~TIconEditApplication()
  145. {
  146. }
  147.  
  148. //----------------------------------------------------------------------------------------
  149. // TIconEditApplication::DoMakeDocument: 
  150. //----------------------------------------------------------------------------------------
  151. #pragma segment AOpen
  152.  
  153. TDocument* TIconEditApplication::DoMakeDocument(CommandNumber/*itsCommandNumber*/ ,
  154.                                                 TFile* itsFile)
  155. {
  156.     TIconDocument * anIconDocument = new TIconDocument;// Create a TIconDocument object.
  157.     anIconDocument->IIconDocument(itsFile);        // Initialize it. 
  158.  
  159.     return anIconDocument;                        // Return a reference to the document.
  160. }
  161.  
  162. //----------------------------------------------------------------------------------------
  163. // TIconEditApplication::MakeViewForAlienClipboard: 
  164. //----------------------------------------------------------------------------------------
  165. #pragma segment AClipboard
  166.  
  167. TView* TIconEditApplication::MakeViewForAlienClipboard()
  168. {
  169.     MAVolatileInit(TIconDocument *, clipDocument, NULL);
  170.  
  171.     // Does the scrap contain my type?
  172.     long offset;
  173.     if (GetScrap(NULL, kIconClipType, &offset) > 0)
  174.     {
  175.         TIconEditView * clipView = NULL;
  176.  
  177.         // Need a document to represent the data to be put on the clipboard. 
  178.         clipDocument = new TIconDocument;
  179.         clipDocument->IIconDocument(NULL);
  180.  
  181.         FailInfo fi;
  182.         Try(fi)                                    // Install failure handler.
  183.         {
  184.             clipDocument->ReturnBitMap()->LoadFromScrap();// Get 'ICON' data from scrap.
  185.  
  186.             clipView = new TIconEditView;
  187.             clipView->IIconEditView(clipDocument, NULL, gZeroVPt, 1);
  188.  
  189.             fi.Success();                        // Don't need failure handler
  190.             // anymore.
  191.         }
  192.         else
  193.         {
  194.             clipDocument = (TIconDocument *)FreeIfObject(clipDocument);// Free
  195.             // clipDocument
  196.             fi.ReSignal();
  197.         }
  198.         return clipView;                        // Return view for Clipboard.
  199.     }
  200.     else
  201.         return Inherited::MakeViewForAlienClipboard();
  202. }
  203.  
  204.  
  205. //========================================================================================
  206. // CLASS TIconDocument
  207. //========================================================================================
  208. #undef Inherited
  209. #define Inherited TFileBasedDocument
  210.  
  211. #pragma segment AOpen
  212. MA_DEFINE_CLASS_M1(TIconDocument,
  213.                    Inherited);
  214.  
  215. //----------------------------------------------------------------------------------------
  216. // TIconDocument constructor 
  217. //----------------------------------------------------------------------------------------
  218. #pragma segment AOpen
  219.  
  220. TIconDocument::TIconDocument() :
  221.     fIconBitMap(NULL),
  222.     // Set this to NULL so that if IDocument
  223.     // fails, ~TIconDocument works okay.
  224.     fIconView(NULL),
  225.     fColor(gRGBBlack)                            // init drawing color to black
  226. {
  227. }
  228. // TIconDocument::TIconDocument 
  229.  
  230. //----------------------------------------------------------------------------------------
  231. // TIconDocument::IIconDocument: 
  232. //----------------------------------------------------------------------------------------
  233. #pragma segment AOpen
  234.  
  235. void TIconDocument::IIconDocument(TFile* itsFile)
  236. {
  237.     IFileBasedDocument(itsFile,            // This document's file 
  238.                              kSignature);        // This document's scrap type 
  239.  
  240.     FailInfo fi;                                // Install failure handler in case we
  241.     Try(fi)                                        // can't create the bit map object.
  242.     {
  243.         TIconBitMap * anIconBitMap = new TIconBitMap;// Allocate a new icon bitmap.
  244.         anIconBitMap->IIconBitMap();            // Initialize it.
  245.         fIconBitMap = anIconBitMap;
  246.  
  247.         fi.Success();                            // Don't need failure handler anymore. 
  248.     }
  249.     else
  250.     {
  251.         Free();                            // Free me too!
  252.         fi.ReSignal();
  253.     }
  254. }
  255.  
  256.  
  257. //----------------------------------------------------------------------------------------
  258. // TIconDocument::Free: 
  259. //----------------------------------------------------------------------------------------
  260. #pragma segment AClose
  261. TIconDocument::~TIconDocument()
  262. {
  263.     fIconBitMap = (TIconBitMap *)FreeIfObject(fIconBitMap);// Dispose of the bitmap if
  264.     // non-NULL.
  265. }
  266.  
  267.  
  268. //----------------------------------------------------------------------------------------
  269. // TIconDocument::DoInitialState: This method is called to set the document's data to the
  270. // "new" state, as when the user chooses to open a new document instead of an existing
  271. // one. We set the value of the document's icon bit map to that of a "seed" icon in our
  272. // resource file. That way we can the document's initial state simply by changing the
  273. // "seed" icon resource.
  274. //----------------------------------------------------------------------------------------
  275. #pragma segment AOpen
  276.  
  277. void TIconDocument::DoInitialState()            // OVERRIDE
  278. {
  279.     Handle seedIcon;
  280.  
  281.     seedIcon = GetIcon(kSeedIconId);            // Get the seed icon resource
  282.     if (seedIcon != NULL)                        // If we got the seed icon resource
  283.         fIconBitMap->SetIconBitMap(seedIcon);
  284. #if qDebug
  285.     else
  286.         ProgramBreak("Unable to get the seed icon resource.");
  287. #endif
  288.  
  289. }
  290.  
  291.  
  292. //----------------------------------------------------------------------------------------
  293. // TIconDocument::DoMakeViews: 
  294. //----------------------------------------------------------------------------------------
  295. #pragma segment AOpen
  296.  
  297. void TIconDocument::DoMakeViews(Boolean forPrinting)// OVERRIDE
  298. {
  299.     TWindow * aWindow;
  300.     TIconEditView * iconView;
  301.  
  302.     if (forPrinting)                            // If for printing, only need the view.
  303.     {
  304.         iconView = (TIconEditView *)gViewServer->DoCreateViews(this, NULL, kIconEditViewId, gZeroVPt);//new
  305.         FailNIL(iconView);
  306.     }
  307.     else
  308.     {                                            // Otherwise need view and window.
  309.         FailNIL(aWindow = gViewServer->NewTemplateWindow(kIconWindowId, this));
  310.         iconView = (TIconEditView *)(aWindow->FindSubView('ICON'));// Get reference to
  311.         // the view.
  312.     }
  313.  
  314.     fIconView = iconView;
  315.  
  316.     TStdPrintHandler * aPrintHandler = new TStdPrintHandler;        // Create a print handler.
  317.     aPrintHandler->IStdPrintHandler(this,        // The document.
  318.                                     iconView);    // The view to be printed.
  319. }
  320.  
  321.  
  322. //----------------------------------------------------------------------------------------
  323. // TIconDocument::DoMenuCommand: 
  324. //----------------------------------------------------------------------------------------
  325. #pragma segment ASelCommand
  326.  
  327. void TIconDocument::DoMenuCommand(CommandNumber aCommandNumber)
  328. {
  329.     switch (aCommandNumber)
  330.     {
  331.         case cInvert:
  332.             TInvertCommand * anInvertCommand = new TInvertCommand;
  333.             anInvertCommand->IInvertCommand(this);
  334.             anInvertCommand->fUseAppleEvent = TRUE;
  335.             PostCommand(anInvertCommand);
  336.             break;
  337.  
  338.         case cZoomIn:
  339.             TZoomInCommand * theZoomInCommand = new TZoomInCommand();
  340.             theZoomInCommand->IZoomInCommand(this);
  341.             theZoomInCommand->fUseAppleEvent = TRUE;
  342.             PostCommand(theZoomInCommand);
  343.             break;
  344.  
  345.         case cZoomOut:
  346.             TZoomOutCommand * theZoomOutCommand = new TZoomOutCommand();
  347.             theZoomOutCommand->IZoomOutCommand(this);
  348.             theZoomOutCommand->fUseAppleEvent = TRUE;
  349.             PostCommand(theZoomOutCommand);
  350.             break;
  351.  
  352.         case cSetColor:
  353.             {
  354.                 CRGBColor newColor;
  355.                 CStr255 thePrompt("Pick a new color");
  356.                 if (GetColor(kBestSystemLocation, thePrompt, fColor, newColor))
  357.                 {
  358.                     if (TOSADispatcher::fgDispatcher->GetDefaultTarget()->IsRecordingOn())
  359.                     {
  360.                         TSetPropertyEvent * appleEvent = new TSetPropertyEvent;
  361.                         appleEvent->ISetPropertyEvent(gServerAddress, kAENoReply, this, pColor);
  362.                         CTempDesc theNewColor;
  363.                         theNewColor.PutRGBColor(newColor);
  364.                         appleEvent->WriteParameter(keyAEData, theNewColor);
  365.                         appleEvent->Send();
  366.                     }
  367.                     else
  368.                     {
  369.                         TSetColorCommand * aSetColorCommand = new TSetColorCommand();
  370.                         aSetColorCommand->ISetColorCommand(this, newColor);
  371.                         PostCommand(aSetColorCommand);
  372.                     }
  373.                 }
  374.             }
  375.             break;
  376.  
  377.         default:
  378.             Inherited::DoMenuCommand(aCommandNumber);
  379.             break;
  380.     }
  381. }
  382.  
  383. //----------------------------------------------------------------------------------------
  384. // TIconDocument::DoScriptCommand    (OVERRIDE)
  385. //----------------------------------------------------------------------------------------
  386.  
  387. void TIconDocument::DoScriptCommand(CommandNumber aCommand,
  388.                                     TAppleEvent* message,
  389.                                     TAppleEvent* reply)
  390. {
  391.     switch (aCommand)
  392.     {
  393.         case cInvert:
  394.             {
  395.                 TInvertCommand * anInvertCommand = new TInvertCommand;
  396.                 anInvertCommand->IInvertCommand(this);
  397.                 PostCommand(anInvertCommand);
  398.             }
  399.             break;
  400.  
  401.         case cZoomIn:
  402.             TZoomInCommand * theZoomInCommand = new TZoomInCommand();
  403.             theZoomInCommand->IZoomInCommand(this);
  404.             PostCommand(theZoomInCommand);
  405.             break;
  406.  
  407.         case cZoomOut:
  408.             TZoomOutCommand * theZoomOutCommand = new TZoomOutCommand();
  409.             theZoomOutCommand->IZoomOutCommand(this);
  410.             PostCommand(theZoomOutCommand);
  411.             break;
  412.  
  413.         case cDrawPointsCommand:
  414.             {
  415.                 TDrawPointsCommand * drawPointsCommand = new TDrawPointsCommand;
  416.                 drawPointsCommand->IDrawPointsCommand(this, message);
  417.                 PostCommand(drawPointsCommand);
  418.             }
  419.             break;
  420.  
  421.         default:
  422.             Inherited::DoScriptCommand(aCommand, message, reply);
  423.             break;
  424.     }
  425. }
  426.  
  427. //----------------------------------------------------------------------------------------
  428. // TIconDocument::DoNeedDiskSpace: 
  429. //----------------------------------------------------------------------------------------
  430. #pragma segment AWriteFile
  431.  
  432. void TIconDocument::DoNeedDiskSpace(TFile* itsFile,
  433.                                     long& dataForkBytes,
  434.                                     long& rsrcForkBytes)//new
  435. {
  436.     Inherited::DoNeedDiskSpace(itsFile, dataForkBytes,// In case parent class saves data.
  437.                                rsrcForkBytes);
  438.  
  439.     dataForkBytes = dataForkBytes +                // Add the size of the icon in bytes and
  440.                    kIconSizeInBytes +            // the space taken up by the color information
  441.                    sizeof(CRGBColor);            // …the number of bytes in the data fork.
  442. }
  443.  
  444.  
  445. //----------------------------------------------------------------------------------------
  446. // TIconDocument::DoRead: 
  447. //----------------------------------------------------------------------------------------
  448. #pragma segment AReadFile
  449.  
  450. void TIconDocument::DoRead(TFile* itsFile,
  451.                            Boolean forPrinting)
  452. {
  453.     Inherited::DoRead(itsFile, forPrinting);    // In case parent class reads data. 
  454.  
  455.     TFileStream * theFileStream = new TFileStream;
  456.  
  457.     theFileStream->IFileStream(itsFile);
  458.     fIconBitMap->ReadFrom(theFileStream);        // Read Data from the file system into 
  459.     // …the icon and handle any errors.
  460.  
  461.     theFileStream->ReadBytes(fColor, sizeof(CRGBColor));// read the color from the stream
  462.  
  463.     theFileStream->Free();
  464. }
  465.  
  466.  
  467. //----------------------------------------------------------------------------------------
  468. // TIconDocument::DoSetupMenus: 
  469. //----------------------------------------------------------------------------------------
  470. #pragma segment ARes
  471.  
  472. void TIconDocument::DoSetupMenus()
  473. {
  474.     Inherited::DoSetupMenus();                    // Set up inherited menus.
  475.     Enable(cInvert, TRUE);                        // The icon can always be inverted.
  476.     Enable(cSetColor, TRUE);                    // The color can always be set
  477. }
  478.  
  479.  
  480. //----------------------------------------------------------------------------------------
  481. // TIconDocument::DoWrite: 
  482. //----------------------------------------------------------------------------------------
  483. #pragma segment AWriteFile
  484.  
  485. void TIconDocument::DoWrite(TFile* itsFile,
  486.                             Boolean makingCopy)
  487. {
  488.     Inherited::DoWrite(itsFile, makingCopy);    // In case parent class writes data. 
  489.  
  490.     TFileStream * theFileStream = new TFileStream;
  491.  
  492.     theFileStream->IFileStream(itsFile);
  493.     fIconBitMap->WriteTo(theFileStream);        // Write data the icon to the file 
  494.     // …and handle any errors.
  495.  
  496.     theFileStream->WriteBytes(&fColor, sizeof(CRGBColor));
  497.  
  498.     theFileStream->Free();
  499. }
  500.  
  501. //----------------------------------------------------------------------------------------
  502. // TIconDocument::InvertIcon: 
  503. //----------------------------------------------------------------------------------------
  504. #pragma segment ADoCommand
  505.  
  506. void TIconDocument::InvertIcon()
  507. {
  508.     fIconBitMap->Invert();                        // Invert the bits of the icon.
  509.     RedrawViews();                        // Make sure all views get redrawn.
  510. }
  511.  
  512.  
  513. //----------------------------------------------------------------------------------------
  514. // TIconDocument::ClearIcon: 
  515. //----------------------------------------------------------------------------------------
  516. #pragma segment ADoCommand
  517.  
  518. void TIconDocument::ClearIcon()
  519. {
  520.     fIconBitMap->Clear();                        // Clear the icon bits.
  521.     RedrawViews();                        // Make sure all views get redrawn.
  522. }
  523.  
  524. //----------------------------------------------------------------------------------------
  525. // TIconDocument::SetIcon: 
  526. //----------------------------------------------------------------------------------------
  527. #pragma segment ADoCommand
  528.  
  529. void TIconDocument::SetIcon(TIconBitMap* newIcon)
  530. {
  531.     if (fIconBitMap != newIcon)
  532.     {
  533.         fIconBitMap = (TIconBitMap *)FreeIfObject(fIconBitMap);
  534.         fIconBitMap = newIcon;                    // Note this doesn't copy the icon.
  535.     }
  536.     RedrawViews();                        // Make sure all views get redrawn.
  537.  
  538. }
  539.  
  540. //----------------------------------------------------------------------------------------
  541. // TIconDocument::RedrawViews: 
  542. //----------------------------------------------------------------------------------------
  543. #pragma segment ARes
  544.  
  545. void TIconDocument::RedrawViews()
  546. {
  547.     fIconView->ForceRedraw();
  548. }
  549.  
  550. //----------------------------------------------------------------------------------------
  551. // TIconDocument::GetSetPropertyInfo:     {OVERRIDE}
  552. //----------------------------------------------------------------------------------------
  553. #pragma segment ARes
  554.  
  555. void TIconDocument::GetSetPropertyInfo(DescType whichProperty,
  556.                                        CommandNumber& cmdNum,
  557.                                        Boolean& canUndo,
  558.                                        Boolean& causesChange,
  559.                                        TCommandHandler*& theContext)
  560. {
  561.     switch (whichProperty)
  562.     {
  563.         case pColor:
  564.             cmdNum = cSetColor;
  565.             canUndo = TRUE;
  566.             causesChange = TRUE;
  567.             theContext = this;
  568.             break;
  569.  
  570.         default:
  571.             Inherited::GetSetPropertyInfo(whichProperty, cmdNum, canUndo, causesChange, theContext);
  572.             break;
  573.     }
  574. }
  575.  
  576.  
  577. //----------------------------------------------------------------------------------------
  578. // TIconDocument::GetObjectProperty:     {OVERRIDE}
  579. //----------------------------------------------------------------------------------------
  580. #pragma segment ARes
  581.  
  582. Boolean TIconDocument::GetObjectProperty(CAEDesc& thePropertyValue,
  583.                                          DescType whichProperty,
  584.                                          const CAEDesc& desiredType)
  585. {
  586.     Boolean hasProperty = TRUE;
  587.  
  588.     switch (whichProperty)
  589.     {
  590.         case pColor:
  591.             thePropertyValue.PutRGBColor(fColor);
  592.             break;
  593.  
  594.         default:
  595.             hasProperty = Inherited::GetObjectProperty(thePropertyValue, whichProperty, desiredType);
  596.             break;
  597.  
  598.     }
  599.  
  600.     return hasProperty;
  601. }
  602.  
  603. //----------------------------------------------------------------------------------------
  604. // TIconDocument::SetObjectProperty:     {OVERRIDE}
  605. //----------------------------------------------------------------------------------------
  606. #pragma segment ARes
  607.  
  608. void TIconDocument::SetObjectProperty(const CAEDesc& thePropertyValue,
  609.                                       DescType whichProperty)
  610. {
  611.     switch (whichProperty)
  612.     {
  613.         case pColor:
  614.             thePropertyValue.GetRGBColor(fColor);
  615.             RedrawViews();
  616.             break;
  617.  
  618.         default:
  619.             Inherited::SetObjectProperty(thePropertyValue, whichProperty);
  620.             break;
  621.     }
  622. }
  623.  
  624.  
  625. //----------------------------------------------------------------------------------------
  626. // TIconDocument::SetIconColor: Set the color of the icon bitmap.
  627. //----------------------------------------------------------------------------------------
  628. #pragma segment ARes
  629.  
  630. void TIconDocument::SetIconColor(CRGBColor& newColor)
  631. {
  632.     fColor = newColor;
  633.     RedrawViews();
  634. }
  635.  
  636. //========================================================================================
  637. // CLASS TIconEditView
  638. //========================================================================================
  639. #undef Inherited
  640. #define Inherited TView
  641.  
  642. #pragma segment AOpen
  643. MA_DEFINE_CLASS_M1(TIconEditView,
  644.                    Inherited);
  645.  
  646. //----------------------------------------------------------------------------------------
  647. // TIconEditView constructor
  648. //----------------------------------------------------------------------------------------
  649. #pragma segment MAConstructorRes
  650.  
  651. TIconEditView::TIconEditView() :
  652.     fMagnification(kDefaultMagnification),
  653.     fIconDocument(NULL)
  654. {
  655. }
  656.  
  657.  
  658. //----------------------------------------------------------------------------------------
  659. // TIconEditView destructor
  660. //----------------------------------------------------------------------------------------
  661. #pragma segment MADestructorRes
  662.  
  663. TIconEditView::~TIconEditView()
  664. {
  665. }
  666.  
  667.  
  668. //----------------------------------------------------------------------------------------
  669. // TIconEditView::IIconEditView: 
  670. //----------------------------------------------------------------------------------------
  671. #pragma segment AOpen
  672.  
  673. void TIconEditView::IIconEditView(TDocument* itsDocument,
  674.                                   TView* itsSuperView,
  675.                                   const VPoint& itsLocation,
  676.                                   short itsMagnification)
  677. {
  678.     IView(itsDocument, itsSuperView, itsLocation, gZeroVPt, sizeVariable, sizeVariable);
  679.  
  680.     fMagnification = itsMagnification;
  681.     fIconDocument = (TIconDocument *)itsDocument;
  682. }
  683.  
  684. //----------------------------------------------------------------------------------------
  685. // TIconEditView::DoPostCreate: 
  686. //----------------------------------------------------------------------------------------
  687. #pragma segment AOpen
  688.  
  689. void TIconEditView::DoPostCreate(TDocument* itsDocument)
  690. {
  691.     fIconDocument = (TIconDocument *)itsDocument;
  692. }
  693.  
  694.  
  695. //----------------------------------------------------------------------------------------
  696. // TIconEditView::CalcMinFrame: 
  697. //----------------------------------------------------------------------------------------
  698. #pragma segment ANonRes
  699.  
  700. VRect TIconEditView::CalcMinFrame()
  701. {
  702.     VRect minFrame(Inherited::CalcMinFrame());
  703.     
  704.     minFrame[botRight] = minFrame[topLeft] + VPoint(kIconHBits * fMagnification + kBorder + kBorder, kIconVBits * fMagnification + kBorder + kBorder);
  705.  
  706.     return minFrame;
  707. }
  708.  
  709. //----------------------------------------------------------------------------------------
  710. // TIconEditView::ContainsClipType: 
  711. //----------------------------------------------------------------------------------------
  712. #pragma segment AClipboard
  713.  
  714. Boolean TIconEditView::ContainsClipType(ResType aType)
  715. {
  716.     return (aType == kIconClipType);
  717. }
  718.  
  719. //----------------------------------------------------------------------------------------
  720. // TIconEditView::DoMenuCommand: 
  721. //----------------------------------------------------------------------------------------
  722. #pragma segment ASelCommand
  723.  
  724. void TIconEditView::DoMenuCommand(CommandNumber aCommandNumber)
  725. {
  726.     switch (aCommandNumber)                        // Decide if this command is ours…
  727.     {
  728.         case cCut:
  729.         case cCopy:
  730.         case cClear:
  731.             {                                    // Post a TIconEditCommand object.
  732.                 TIconEditCommand * anIconEditCommand = new TIconEditCommand;
  733.                 anIconEditCommand->IIconEditCommand(aCommandNumber, this, fIconDocument);
  734.                 PostCommand(anIconEditCommand);
  735.             }
  736.  
  737.         case cPaste:
  738.             {                                    // Post a TIconPasteCommand object.
  739.                 TIconPasteCommand * anIconPasteCommand = new TIconPasteCommand;
  740.                 anIconPasteCommand->IIconPasteCommand(this, fIconDocument);
  741.                 PostCommand(anIconPasteCommand);
  742.             }
  743.  
  744.         default:                                // Otherwise, let someone else handle it.
  745.             Inherited::DoMenuCommand(aCommandNumber);
  746.     }
  747.  
  748.     return;
  749. }
  750.  
  751.  
  752. //----------------------------------------------------------------------------------------
  753. // TIconEditView::DoKeyEvent: 
  754. //----------------------------------------------------------------------------------------
  755. #pragma segment ASelCommand
  756.  
  757. void TIconEditView::DoKeyEvent(TToolboxEvent* info)
  758. {
  759.     if (info->fText == chBackspace)
  760.     {
  761.         // Post a TIconEditCommand object.
  762.         TIconEditCommand * anIconEditCommand = new TIconEditCommand;
  763.         anIconEditCommand->IIconEditCommand(cClear, this, fIconDocument);// Same as Clear menu command.
  764.         PostCommand(anIconEditCommand);
  765.     }
  766.     else
  767.         Inherited::DoKeyEvent(info);            // In case someone else wants it. 
  768. }
  769.  
  770.  
  771. //----------------------------------------------------------------------------------------
  772. // TIconEditView::DoMouseCommand: 
  773. //----------------------------------------------------------------------------------------
  774. #pragma segment ASelCommand
  775.  
  776. void TIconEditView::DoMouseCommand(VPoint& theMouse,
  777.                                    TToolboxEvent*/*event*/  ,
  778.                                    CPoint        /*hysteresis*/)
  779. {
  780.     VPoint iconBit;
  781.     if (PointToBit(theMouse, iconBit))    // If CPoint is within the icon
  782.     {
  783.         TIconDrawCommand * anIconDrawCommand = new TIconDrawCommand;// …then make a drawing command.
  784.         anIconDrawCommand->IIconDrawCommand(this, fIconDocument, theMouse);
  785.         PostCommand(anIconDrawCommand);    // and Post it.
  786.     }
  787. }
  788.  
  789. //----------------------------------------------------------------------------------------
  790. // TIconEditView::DoSetCursor: 
  791. //----------------------------------------------------------------------------------------
  792. #pragma segment ARes
  793.  
  794. void TIconEditView::DoSetCursor(const VPoint& localPoint,
  795.                                 RgnHandle cursorRegion)//new
  796. {
  797.  
  798.     CTemporaryRegion outerRegion;
  799.     CTemporaryRegion innerRegion;
  800.  
  801.     GetExtentRegion(outerRegion);
  802.     CopyRgn(outerRegion, innerRegion);
  803.     InsetRgn(innerRegion, kBorder, kBorder);
  804.  
  805.     VPoint iconBit;
  806.     if (PointToBit(localPoint, iconBit))    // If the cursor is not in the borders…
  807.     {
  808.         CursHandle pencilCursor = GetCursor(GetCursorID());// Attempt to get the pencil cursor.
  809.         FailNILResource((Handle)pencilCursor);
  810.         SetCursor(*pencilCursor);                // Set the cursor on the screen.
  811.         CopyRgn(innerRegion, cursorRegion);
  812.     }
  813.     else
  814.     {
  815.         SetCursor(&(qd.arrow));                    // Set the cursor on the screen. 
  816.         DiffRgn(outerRegion, innerRegion, cursorRegion);
  817.     }
  818. }
  819.  
  820.  
  821. //----------------------------------------------------------------------------------------
  822. // TIconEditView::DoSetupMenus: 
  823. //----------------------------------------------------------------------------------------
  824. #pragma segment ARes
  825.  
  826. void TIconEditView::DoSetupMenus()
  827. {
  828.     Inherited::DoSetupMenus();                    // Set up inherited menus.
  829.  
  830.     Enable(cZoomIn, TRUE);                        // Can always zoom in.
  831.     Enable(cZoomOut, fMagnification > 1);        // Can zoom out if not at smallest size.
  832.     Enable(cSetColor, TRUE);                    // can always set the color
  833.     Enable(cCut, TRUE);                            // Always enable the Cut command.
  834.     Enable(cCopy, TRUE);                        // Always enable the Copy command.
  835.     Enable(cClear, TRUE);                        // Always enable the Clear command.
  836.     gClipboardMgr->CanPaste(kIconClipType);        // This view can paste 'ICON' data.
  837. }
  838.  
  839.  
  840. //----------------------------------------------------------------------------------------
  841. // TIconEditView::Draw: 
  842. //----------------------------------------------------------------------------------------
  843. #pragma segment ARes
  844.  
  845. void TIconEditView::Draw(const VRect&            /*area*/ )//new
  846. {
  847.     CRect drawingRect(ViewToQDRect(VRect(kBorder, kBorder, kBorder + (kIconHBits * fMagnification), kBorder + (kIconVBits * fMagnification))));
  848.  
  849.     fIconDocument->ReturnBitMap()->Draw(drawingRect, fIconDocument->GetIconColor());
  850. }
  851.  
  852. //----------------------------------------------------------------------------------------
  853. // TIconEditView::DrawBit: 
  854. //----------------------------------------------------------------------------------------
  855. #pragma segment ARes
  856.  
  857. void TIconEditView::DrawBit(VPoint theBit,
  858.                             Boolean turnBitOn,
  859.                             CRGBColor& drawingColor)
  860. {
  861.     CRect theRect(kBorder + ((short)theBit.h) * fMagnification, kBorder + ((short)theBit.v) * fMagnification, kBorder + ((short)(theBit.h + 1)) * fMagnification, kBorder + ((short)(theBit.v + 1)) * fMagnification);
  862.     // Setup the bit's CRect the in edit view.
  863.  
  864.     if (turnBitOn)
  865.     {
  866.         if (qNeedsColorQD || HasColorQD())
  867.         {
  868.             CRGBColor oldColor(GetIfColor());
  869.             SetIfColor(drawingColor);
  870.  
  871.             PenNormal();
  872.             PaintRect(theRect);
  873.             SetIfColor(oldColor);
  874.         }
  875.         else
  876.             FillRect(theRect, &qd.black);
  877.     }
  878.     else
  879.         FillRect(theRect, &qd.white);
  880. }
  881.  
  882. //----------------------------------------------------------------------------------------
  883. // TIconEditView::PointToBit:  Converts the given mouse CPoint to an icon bit. 
  884. //----------------------------------------------------------------------------------------
  885. #pragma segment ARes
  886.  
  887. Boolean TIconEditView::PointToBit(const VPoint& thePoint,
  888.                                   VPoint& iconBit)
  889. {
  890.     Boolean returnVal = FALSE;
  891.  
  892.     VPoint aPoint(thePoint);                    // use a copy, since thePoint is a const
  893.     aPoint.h = aPoint.h - kBorder;                // Account for border in the edit view.
  894.     aPoint.v = aPoint.v - kBorder;
  895.  
  896.     // If the mouse is within the edit view's icon…
  897.     if ((aPoint.h >= 0) && (aPoint.h < kIconHBits * fMagnification) && (aPoint.v >= 0) && (aPoint.v < kIconVBits * fMagnification))
  898.     {
  899.         iconBit.h = aPoint.h / fMagnification;    // Convert from edit view CPoint
  900.         iconBit.v = aPoint.v / fMagnification;    // …to icon bit.
  901.         returnVal = TRUE;
  902.     }
  903.  
  904.     return returnVal;
  905. }
  906.  
  907.  
  908. //----------------------------------------------------------------------------------------
  909. // TIconEditView::SetMagnification: 
  910. //----------------------------------------------------------------------------------------
  911. #pragma segment ADoCommand
  912.  
  913. void TIconEditView::SetMagnification(short magnification)
  914. {
  915.     fMagnification = (short)Max(1, magnification);// Set the new magnification.
  916.     AdjustFrame();                        // Magnification affects the view's size.
  917.     ForceRedraw();                        // Force the view to be entirely redrawn.
  918. }
  919.  
  920.  
  921. //----------------------------------------------------------------------------------------
  922. // TIconEditView::WriteToDeskScrap: 
  923. //----------------------------------------------------------------------------------------
  924. #pragma segment AClipboard
  925.  
  926. void TIconEditView::WriteToDeskScrap()
  927. {
  928.     fIconDocument->ReturnBitMap()->WriteToScrap();// Write the icon data to the clipboard.
  929.     Inherited::WriteToDeskScrap();                // Write 'PICT' data to the desk scrap.
  930. }
  931.  
  932.  
  933.  
  934. //========================================================================================
  935. // CLASS TDrawLineAppleEvent
  936. //========================================================================================
  937. #undef Inherited
  938. #define Inherited TAppleEvent
  939.  
  940. #pragma segment ASelCommand
  941. MA_DEFINE_CLASS_M1(TDrawPointsAppleEvent,
  942.                    Inherited);
  943.  
  944. //----------------------------------------------------------------------------------------
  945. // TDrawLineAppleEvent    constructor
  946. //----------------------------------------------------------------------------------------
  947. #pragma segment ASelCommand
  948.  
  949. TDrawPointsAppleEvent::TDrawPointsAppleEvent()
  950. {
  951.     // nothing to do
  952. }
  953.  
  954. //----------------------------------------------------------------------------------------
  955. // TDrawPointsAppleEvent destructor
  956. //----------------------------------------------------------------------------------------
  957. #pragma segment MADestructorRes
  958.  
  959. TDrawPointsAppleEvent::~TDrawPointsAppleEvent()
  960. {
  961. }
  962.  
  963. //----------------------------------------------------------------------------------------
  964. // TDrawPointsAppleEvent::IDrawPointsAppleEvent
  965. //----------------------------------------------------------------------------------------
  966. #pragma segment ASelCommand
  967.  
  968. void TDrawPointsAppleEvent::IDrawPointsAppleEvent(MScriptableObject* itsDirectObject,
  969.                                                   TIconBitMap* iconBitMapModel,
  970.                                                   const Boolean turnBitsOn)
  971. {
  972.     CTempDesc directObjectDesc;
  973.     VPoint iconBit;
  974.     CPoint qdPoint;
  975.  
  976.  
  977.     IAppleEvent(kAEIconEditClass, kAEDrawPoints, gServerAddress, kAENoReply + kAEDontExecute);
  978.     itsDirectObject->MakeObjectSpecifier(directObjectDesc, formUniqueID);
  979.     WriteParameter(keyDirectObject, directObjectDesc);
  980.  
  981.     TDynamicArray * pointArray = new TDynamicArray;// create an array to store a list of points
  982.     pointArray->IDynamicArray(1, sizeof(CPoint));
  983.  
  984.     // add the points set in the model to the dynamic array
  985.     for (short vCoord = 0; vCoord < kIconVBits; ++vCoord)
  986.     {
  987.         iconBit.v = vCoord;
  988.         for (short hCoord = 0; hCoord < kIconHBits; ++hCoord)
  989.         {
  990.             iconBit.h = hCoord;
  991.             if (iconBitMapModel->GetBit(iconBit))
  992.             {
  993.                 qdPoint = iconBit.ToPoint();    // convert the iconBit to a CPoint
  994.                 pointArray->InsertElementsBefore(pointArray->GetSize() + 1, &qdPoint, 1);
  995.             }
  996.         }
  997.     }
  998.  
  999.     // write the point list to the apple event parameter list
  1000.     WritePtrList(keyPointList, typeQDPoint, pointArray);
  1001.  
  1002.     pointArray = (TDynamicArray *)FreeIfObject(pointArray);
  1003.  
  1004.     // if turning the points off, add an optional parameter
  1005.     if (!turnBitsOn)
  1006.         WriteBoolean(keyErasePoints, TRUE);
  1007. }
  1008.  
  1009. //========================================================================================
  1010. // CLASS TInvertCommand
  1011. //========================================================================================
  1012. #undef Inherited
  1013. #define Inherited TCommand
  1014.  
  1015. #pragma segment ASelCommand
  1016. MA_DEFINE_CLASS_M1(TInvertCommand,
  1017.                    Inherited);
  1018.  
  1019. //----------------------------------------------------------------------------------------
  1020. // TInvertCommand constructor 
  1021. //----------------------------------------------------------------------------------------
  1022. #pragma segment ASelCommand
  1023.  
  1024. TInvertCommand::TInvertCommand() :
  1025.     fIconDocument(NULL)
  1026. {
  1027. }
  1028. // TInvertCommand::TInvertCommand 
  1029.  
  1030. //----------------------------------------------------------------------------------------
  1031. // TInvertCommand destructor
  1032. //----------------------------------------------------------------------------------------
  1033. #pragma segment MADestructorRes
  1034.  
  1035. TInvertCommand::~TInvertCommand()
  1036. {
  1037. }
  1038.  
  1039. //----------------------------------------------------------------------------------------
  1040. // TInvertCommand::IInvertCommand: 
  1041. //----------------------------------------------------------------------------------------
  1042. #pragma segment ASelCommand
  1043.  
  1044. void TInvertCommand::IInvertCommand(TIconDocument* itsIconDocument)
  1045. {
  1046.     ICommand(cInvert,                        // Initialize the inherited data…
  1047.                    itsIconDocument, kCanUndo, kCausesChange, itsIconDocument);// …no view or scroller to track.
  1048.     fIconDocument = itsIconDocument;            // Save a reference to the icon document.
  1049. }
  1050.  
  1051. //----------------------------------------------------------------------------------------
  1052. // TInvertCommand::MakeAppleEvent 
  1053. //----------------------------------------------------------------------------------------
  1054. TAppleEvent* TInvertCommand::MakeAppleEvent()
  1055. {
  1056.     CTempDesc directObjectDesc;
  1057.     TAppleEvent * theInvertEvent = new TAppleEvent;
  1058.  
  1059.     theInvertEvent->IAppleEvent(kAEIconEditClass, kAEInvertID, gServerAddress, kAENoReply);
  1060.     fIconDocument->MakeObjectSpecifier(directObjectDesc, fIconDocument->GetSpecifierForm());// create a descriptor
  1061.     theInvertEvent->WriteParameter(keyDirectObject, directObjectDesc);
  1062.  
  1063.     return theInvertEvent;
  1064. }
  1065.  
  1066. //----------------------------------------------------------------------------------------
  1067. // TInvertCommand::DoIt: 
  1068. //----------------------------------------------------------------------------------------
  1069. #pragma segment ADoCommand
  1070.  
  1071. void TInvertCommand::DoIt()
  1072. {
  1073.     fIconDocument->InvertIcon();                // Invert the document's icon bitmap.
  1074. }
  1075.  
  1076.  
  1077. //----------------------------------------------------------------------------------------
  1078. // TInvertCommand::UndoIt: 
  1079. //----------------------------------------------------------------------------------------
  1080. #pragma segment ADoCommand
  1081.  
  1082. void TInvertCommand::UndoIt()
  1083. {
  1084.     fIconDocument->InvertIcon();                // Uninvert the document's icon bitmap.
  1085. }
  1086.  
  1087.  
  1088. //----------------------------------------------------------------------------------------
  1089. // TInvertCommand::RedoIt: 
  1090. //----------------------------------------------------------------------------------------
  1091. #pragma segment ADoCommand
  1092.  
  1093. void TInvertCommand::RedoIt()
  1094. {
  1095.     fIconDocument->InvertIcon();                // Reinvert the document's icon bitmap.
  1096. }
  1097.  
  1098. //========================================================================================
  1099. // CLASS TDrawPointsCommand
  1100. //========================================================================================
  1101. #undef Inherited
  1102. #define Inherited TCommand
  1103.  
  1104. #pragma segment ASelCommand
  1105. MA_DEFINE_CLASS_M1(TDrawPointsCommand,
  1106.                    Inherited);
  1107.  
  1108. //----------------------------------------------------------------------------------------
  1109. // TSetPointsCommand constructor 
  1110. //----------------------------------------------------------------------------------------
  1111. #pragma segment ASelCommand
  1112.  
  1113. TDrawPointsCommand::TDrawPointsCommand() :
  1114.     fSavedBitMap(NULL),
  1115.     fIconDocument(NULL),
  1116.     fErasePoints(FALSE)                            // by default, turn bits on
  1117. {
  1118. }
  1119. // TDrawPointsCommand::TDrawPointsCommand
  1120.  
  1121. //----------------------------------------------------------------------------------------
  1122. // TDrawPointsCommand::IDrawPointsCommand
  1123. //----------------------------------------------------------------------------------------
  1124. #pragma segment ASelCommand
  1125.  
  1126. void TDrawPointsCommand::IDrawPointsCommand(TIconDocument* itsIconDocument,
  1127.                                             TAppleEvent* theAppleEvent)
  1128. {
  1129.     fIconDocument = itsIconDocument;
  1130.  
  1131.     ICommand(cDrawPointsCommand,            // Initialize the inherited data…
  1132.                    itsIconDocument, kCanUndo, kCausesChange, itsIconDocument);
  1133.  
  1134.     // read the required pointlist parameter
  1135.     fPointList = new TDynamicArray;
  1136.     fPointList->IDynamicArray(1, sizeof(CPoint));
  1137.  
  1138.     theAppleEvent->ReadPtrList(keyPointList, typeQDPoint, fPointList);
  1139.  
  1140.     // read the optional erasePoints parameter
  1141.     if (theAppleEvent->HasParameter(keyErasePoints))
  1142.         fErasePoints = theAppleEvent->ReadBoolean(keyErasePoints);
  1143. }
  1144.  
  1145.  
  1146.  
  1147. //----------------------------------------------------------------------------------------
  1148. // TDrawPointsCommand::DoIt
  1149. //----------------------------------------------------------------------------------------
  1150. #pragma segment ADoCommand
  1151.  
  1152. void TDrawPointsCommand::DoIt()
  1153. {
  1154.     TIconBitMap * targetBitMap = fIconDocument->ReturnBitMap();
  1155.     fSavedBitMap = targetBitMap->Copy();        // save the current bitmap
  1156.  
  1157.     // draw the points listed in the pointlist
  1158.     CArrayIterator iter(fPointList);
  1159.     CPoint aPoint;
  1160.     VPoint iconBit;
  1161.  
  1162.     for (ArrayIndex i = iter.FirstIndex(); iter.More(); i = iter.NextIndex())
  1163.     {
  1164.         fPointList->GetElementsAt(i, (Point *)aPoint, 1);
  1165.         iconBit.h = aPoint.h;
  1166.         iconBit.v = aPoint.v;
  1167.         targetBitMap->SetBit(iconBit, !fErasePoints);// set the bit in the bitmap
  1168.     }
  1169.  
  1170.     fIconDocument->SetIcon(targetBitMap);        // this will cause a redraw
  1171.     // free the point list…it won't be used again
  1172.     fPointList = (TDynamicArray *)FreeIfObject(fPointList);
  1173.  
  1174. }
  1175.  
  1176. //----------------------------------------------------------------------------------------
  1177. // TDrawPointsCommand::UndoIt
  1178. //----------------------------------------------------------------------------------------
  1179. #pragma segment ADoCommand
  1180.  
  1181. void TDrawPointsCommand::UndoIt()
  1182. {
  1183.     TIconBitMap * tempBitMap = fIconDocument->ReturnBitMap()->Copy();// save a local copy
  1184.     fIconDocument->SetIcon(fSavedBitMap);
  1185.     fSavedBitMap = tempBitMap;
  1186.     //fIconDocument->SetIcon(fOriginalBitMap);
  1187. }
  1188.  
  1189.  
  1190. //----------------------------------------------------------------------------------------
  1191. // TDrawPointsCommand::RedoIt
  1192. //----------------------------------------------------------------------------------------
  1193. #pragma segment ADoCommand
  1194.  
  1195. void TDrawPointsCommand::RedoIt()
  1196. {
  1197.     TIconBitMap * tempBitMap = fIconDocument->ReturnBitMap()->Copy();// save a local copy
  1198.     fIconDocument->SetIcon(fSavedBitMap);
  1199.     fSavedBitMap = tempBitMap;
  1200. }
  1201.  
  1202.  
  1203. //----------------------------------------------------------------------------------------
  1204. // TDrawLineCommand::Free
  1205. //----------------------------------------------------------------------------------------
  1206. #pragma segment ADoCommand
  1207.  
  1208. TDrawPointsCommand::~TDrawPointsCommand()
  1209. {
  1210.     if (fCommandDone)
  1211.         fSavedBitMap = (TIconBitMap *)FreeIfObject(fSavedBitMap);
  1212.  
  1213.     fPointList = (TDynamicArray *)FreeIfObject(fPointList);
  1214. }
  1215.  
  1216. //========================================================================================
  1217. // CLASS TSetColorCommand
  1218. //========================================================================================
  1219. #undef Inherited
  1220. #define Inherited TCommand
  1221.  
  1222. #pragma segment ASelCommand
  1223. MA_DEFINE_CLASS_M1(TSetColorCommand,
  1224.                    Inherited);
  1225.  
  1226. //----------------------------------------------------------------------------------------
  1227. // TSetColorCommand constructor 
  1228. //----------------------------------------------------------------------------------------
  1229. #pragma segment ASelCommand
  1230.  
  1231. TSetColorCommand::TSetColorCommand()
  1232. {
  1233. }
  1234.  
  1235. //----------------------------------------------------------------------------------------
  1236. // TSetColorCommand destructor
  1237. //----------------------------------------------------------------------------------------
  1238. #pragma segment MADestructorRes
  1239.  
  1240. TSetColorCommand::~TSetColorCommand()
  1241. {
  1242. }
  1243.  
  1244. //----------------------------------------------------------------------------------------
  1245. // TSetColorCommand::ISetColorCommand
  1246. //----------------------------------------------------------------------------------------
  1247. #pragma segment ASelCommand
  1248.  
  1249. void TSetColorCommand::ISetColorCommand(TIconDocument* itsDocument,
  1250.                                         CRGBColor& newColor)
  1251. {
  1252.     ICommand(cSetColor,                    // Initialize the command…
  1253.                    itsDocument,                    // Associate it with a document.
  1254.                    kCanUndo, kCausesChange, itsDocument);
  1255.     fNewColor = newColor;
  1256.     fOldColor = itsDocument->GetIconColor();
  1257. }
  1258.  
  1259. //----------------------------------------------------------------------------------------
  1260. // TSetColorCommand::DoIt
  1261. //----------------------------------------------------------------------------------------
  1262. #pragma segment ADoCommand
  1263.  
  1264. void TSetColorCommand::DoIt()
  1265. {
  1266.     ((TIconDocument *)fContext)->SetIconColor(fNewColor);
  1267. }
  1268.  
  1269. //----------------------------------------------------------------------------------------
  1270. // TSetColorCommand::UndoIt
  1271. //----------------------------------------------------------------------------------------
  1272. #pragma segment ADoCommand
  1273.  
  1274. void TSetColorCommand::UndoIt()
  1275. {
  1276.     ((TIconDocument *)fContext)->SetIconColor(fOldColor);
  1277. }
  1278.  
  1279. //----------------------------------------------------------------------------------------
  1280. // TSetColorCommand::RedoIt
  1281. //----------------------------------------------------------------------------------------
  1282. #pragma segment ADoCommand
  1283.  
  1284. void TSetColorCommand::RedoIt()
  1285. {
  1286.     ((TIconDocument *)fContext)->SetIconColor(fNewColor);
  1287. }
  1288.  
  1289.  
  1290. //========================================================================================
  1291. // CLASS TIconDrawCommand
  1292. //========================================================================================
  1293. #undef Inherited
  1294. #define Inherited TTracker
  1295.  
  1296. #pragma segment ASelCommand
  1297. MA_DEFINE_CLASS_M1(TIconDrawCommand,
  1298.                    Inherited);
  1299.  
  1300. //----------------------------------------------------------------------------------------
  1301. // TIconDrawCommand constructor 
  1302. //----------------------------------------------------------------------------------------
  1303. #pragma segment ASelCommand
  1304.  
  1305. TIconDrawCommand::TIconDrawCommand() :
  1306.     fIconDocument(NULL),
  1307.     fIconEditView(NULL),
  1308.     fIconBitMap(NULL),
  1309.     fOriginalIcon(NULL),
  1310.     fTurnBitsOn(TRUE),
  1311.     fIconBitMapModel(NULL)
  1312. {
  1313. }
  1314. // TIconDrawCommand::TIconDrawCommand 
  1315.  
  1316. //----------------------------------------------------------------------------------------
  1317. // TIconDrawCommand::IIconDrawCommand: 
  1318. //----------------------------------------------------------------------------------------
  1319. #pragma segment ASelCommand
  1320.  
  1321. void TIconDrawCommand::IIconDrawCommand(TIconEditView* itsIconEditView,
  1322.                                         TIconDocument* itsIconDocument,
  1323.                                         VPoint& theMouse)
  1324. {
  1325.     ITracker(cDrawCommand,                // Initialize the command… 
  1326.                    itsIconDocument,                // Its context
  1327.                    kCanUndo, kCausesChange, itsIconDocument,// Associate it with a notifying object.
  1328.                    itsIconEditView,                // Associate it with a view.
  1329.                    itsIconEditView->GetScroller(TRUE),// Associate it with a scroller
  1330.                    theMouse);
  1331.     fConstrainsMouse = TRUE;                    // Want TrackConstrain called.
  1332.  
  1333.     fIconEditView = itsIconEditView;            // Set some convenience fields…
  1334.     fIconDocument = itsIconDocument;
  1335.     fIconBitMap = fIconDocument->ReturnBitMap();// Get reference to icon being drawn.
  1336.     fIconBitMapModel = new TIconBitMap;            // create a model for the bits being set
  1337.     fIconBitMapModel->IIconBitMap();
  1338.     fIconBitMapModel->Clear();
  1339.  
  1340.     fColor = fIconDocument->GetIconColor();        // store the drawing color
  1341. }
  1342.  
  1343.  
  1344. //----------------------------------------------------------------------------------------
  1345. // TIconDrawCommand::Free: 
  1346. //----------------------------------------------------------------------------------------
  1347. #pragma segment ADoCommand
  1348.  
  1349. TIconDrawCommand::~TIconDrawCommand()
  1350. {
  1351.     if (fCommandDone)                            // If the command is done…
  1352.         fOriginalIcon = (TIconBitMap *)FreeIfObject(fOriginalIcon);// …dispose of original icon.
  1353.     else                                        // Else if command is undone…
  1354.         fIconBitMap = (TIconBitMap *)FreeIfObject(fIconBitMap);// …dispose of icon that was drawn
  1355.  
  1356.     fIconBitMapModel = (TIconBitMap *)FreeIfObject(fIconBitMapModel);// free the bitmap model
  1357. }
  1358.  
  1359. //----------------------------------------------------------------------------------------
  1360. // TIconDrawCommand::TrackConstrain: 
  1361. //----------------------------------------------------------------------------------------
  1362. #pragma segment ADoCommand
  1363.  
  1364. void TIconDrawCommand::TrackConstrain(TrackPhase/*aTrackPhase*/ ,
  1365.                                       const VPoint&/*anchorPoint*/  ,
  1366.                                       const VPoint&/*previousPoint*/  ,
  1367.                                       VPoint& nextPoint,
  1368.                                       Boolean    /*mouseDidMove*/)
  1369. {
  1370.     // This is on several lines so that Max can be "inlined"
  1371.     VCoordinate h = Min(nextPoint.h, fIconEditView->fSize.h - kBorder - 1);
  1372.     VCoordinate v = Min(nextPoint.v, fIconEditView->fSize.v - kBorder - 1);
  1373.     h = Max(h, (long)kBorder);
  1374.     v = Max(v, (long)kBorder);
  1375.     nextPoint = VPoint(h, v);
  1376. }
  1377.  
  1378.  
  1379. //----------------------------------------------------------------------------------------
  1380. // TIconDrawCommand::TrackFeedback: 
  1381. //----------------------------------------------------------------------------------------
  1382. #pragma segment ADoCommand
  1383.  
  1384. void TIconDrawCommand::TrackFeedback(TrackPhase    /*aTrackPhase*/,
  1385.                                      const VPoint&/*anchorPoint*/  ,
  1386.                                      const VPoint&/*previousPoint*/  ,
  1387.                                      const VPoint&/*nextPoint*/  ,
  1388.                                      Boolean    /*mouseDidMove*/,
  1389.                                      Boolean    /*turnItOn*/)
  1390. {
  1391.     // Overridden to avoid standard feedback.
  1392. }
  1393.  
  1394.  
  1395. //----------------------------------------------------------------------------------------
  1396. // TIconDrawCommand::TrackMouse: 
  1397. //----------------------------------------------------------------------------------------
  1398. #pragma segment ADoCommand
  1399.  
  1400. TTracker* TIconDrawCommand::TrackMouse(TrackPhase aTrackPhase,
  1401.                                        VPoint&    /*anchorPoint*/ ,
  1402.                                        VPoint& previousPoint,
  1403.                                        VPoint& nextPoint,
  1404.                                        Boolean mouseDidMove)
  1405. {
  1406.     VPoint fromBit;
  1407.     VPoint toBit;
  1408.     VPoint iconBit;
  1409.     short lineLength;
  1410.     float deltaH;
  1411.     float deltaV;
  1412.     float h;
  1413.     float v;
  1414.  
  1415.     // Convert nextPoint and previousPoint to bit locations in the icon…
  1416.     fIconEditView->PointToBit(fIconEditView->ViewToQDPt(nextPoint), toBit);
  1417.     fIconEditView->PointToBit(fIconEditView->ViewToQDPt(previousPoint), fromBit);
  1418.  
  1419.     if (aTrackPhase == trackBegin)                // first time through
  1420.     {
  1421.         fTurnBitsOn = !fIconBitMap->GetBit(toBit);// Turn bits on or off?
  1422.         fOriginalIcon = fIconBitMap->Copy();    // Make a copy of the original for undo
  1423.     }
  1424.  
  1425.     if (mouseDidMove)                            // If mouse moved since last time…
  1426.     {
  1427.         // The following sets bits in the icon from the bit at previousPoint to the bit
  1428.         // at nextPoint.  It is based on a simple line-drawing algorithm.
  1429.  
  1430.         lineLength = (short)Max(labs(toBit.h - fromBit.h), labs(toBit.v - fromBit.v));
  1431.         lineLength = (short)Max(1, lineLength);    // This is on two lines so that Max can be "inlined"
  1432.  
  1433.         deltaH = (toBit.h - fromBit.h) / lineLength;
  1434.         deltaV = (toBit.v - fromBit.v) / lineLength;
  1435.  
  1436.         h = fromBit.h + 0.5;
  1437.         v = fromBit.v + 0.5;
  1438.  
  1439.         for (short i = 0; i < lineLength; i++)
  1440.         {
  1441.             iconBit.h = h;
  1442.             iconBit.v = v;
  1443.  
  1444.             fIconBitMap->SetBit(iconBit, fTurnBitsOn);// Set the bit in the icon
  1445.             fIconEditView->DrawBit(iconBit, fTurnBitsOn, fColor);// …and draw it in the edit view.
  1446.  
  1447.             // set the bit in the bitMapView model.  this model will be used to generate
  1448.             // a setpoints apple event for recordability.
  1449.             fIconBitMapModel->SetBit(iconBit, TRUE);
  1450.  
  1451.             h = h + deltaH;
  1452.             v = v + deltaV;
  1453.         }
  1454.     }
  1455.  
  1456.     if (aTrackPhase == trackEnd)                // when tracking is done, post a
  1457.     {
  1458.         // recordable apple event            
  1459.         TDrawPointsAppleEvent * aDrawEvent = new TDrawPointsAppleEvent;
  1460.         aDrawEvent->IDrawPointsAppleEvent(fIconDocument, fIconBitMapModel, fTurnBitsOn);
  1461.         aDrawEvent->Send();
  1462.     }
  1463.     return this;                                // Return same command object.
  1464. }
  1465.  
  1466. //----------------------------------------------------------------------------------------
  1467. // TIconDrawCommand::DoIt:
  1468. //----------------------------------------------------------------------------------------
  1469. #pragma segment ADoCommand
  1470.  
  1471. void TIconDrawCommand::DoIt()
  1472. {
  1473.     fIconDocument->SetIcon(fIconBitMap);        // Set the document's bit map
  1474.  
  1475.     // SetIcon invalidates all views of the document, including the one we just drew in.
  1476.     // To avoid flashing, validate the edit view here so it doesn't get redrawn.
  1477.  
  1478.     fIconEditView->ValidateVRect(fIconEditView->GetExtent());
  1479. }
  1480.  
  1481. //----------------------------------------------------------------------------------------
  1482. // TIconDrawCommand::UndoIt: 
  1483. //----------------------------------------------------------------------------------------
  1484. #pragma segment ADoCommand
  1485.  
  1486. void TIconDrawCommand::UndoIt()
  1487. {
  1488.     // Save a local copy of the current bit map, and reset it after the call to
  1489.     // SetIcon.  This is necessary since SetIcon frees the current bitmap    
  1490.     TIconBitMap * tempBitMap = fIconBitMap->Copy();
  1491.     fIconDocument->SetIcon(fOriginalIcon);        // Restore icon to original state.
  1492.     fIconBitMap = tempBitMap;
  1493. }
  1494.  
  1495. //----------------------------------------------------------------------------------------
  1496. // TIconDrawCommand::RedoIt: 
  1497. //----------------------------------------------------------------------------------------
  1498. #pragma segment ADoCommand
  1499.  
  1500. void TIconDrawCommand::RedoIt()
  1501. {
  1502.     // save a local copy of the current bit map, and reset it after the call to
  1503.     // SetIcon.  This is necessary since SetIcon frees the current bitmap
  1504.     TIconBitMap * tempBitMap = fOriginalIcon->Copy();
  1505.     fIconDocument->SetIcon(fIconBitMap);        // Set the icon back to the new one.
  1506.     fOriginalIcon = tempBitMap;
  1507. }
  1508.  
  1509.  
  1510. //========================================================================================
  1511. // CLASS TIconEditCommand
  1512. //========================================================================================
  1513. #undef Inherited
  1514. #define Inherited TCommand
  1515.  
  1516. #pragma segment ASelCommand
  1517. MA_DEFINE_CLASS_M1(TIconEditCommand,
  1518.                    Inherited);
  1519.  
  1520. //----------------------------------------------------------------------------------------
  1521. // TIconEditCommand::Initialize: 
  1522. //----------------------------------------------------------------------------------------
  1523. #pragma segment ASelCommand
  1524.  
  1525. TIconEditCommand::TIconEditCommand() :
  1526.     fIconDocument(NULL),
  1527.     fIconEditView(NULL),
  1528.     fSavedBitMap(NULL)
  1529. {
  1530. }
  1531. // TIconEditCommand::TIconEditCommand 
  1532.  
  1533. //----------------------------------------------------------------------------------------
  1534. // TIconEditCommand::IIconEditCommand: 
  1535. //----------------------------------------------------------------------------------------
  1536. #pragma segment ASelCommand
  1537.  
  1538. void TIconEditCommand::IIconEditCommand(CommandNumber itsCommandNumber,
  1539.                                         TIconEditView* itsIconEditView,
  1540.                                         TIconDocument* itsIconDocument)
  1541.  
  1542. {
  1543.     ICommand(itsCommandNumber,            // Initialize the command…
  1544.                    itsIconDocument,                // Associate it with a document.
  1545.                    kCanUndo, kCausesChange, itsIconDocument);
  1546.  
  1547.     fChangesClipboard = (itsCommandNumber != cClear);// Cut or Copy changes the clipboard.            
  1548.     fCausesChange = (itsCommandNumber != cCopy);// Cut or Clear changes the documnt.
  1549.  
  1550.     fIconEditView = itsIconEditView;            // Remember reference to the view.
  1551.     fIconDocument = itsIconDocument;            // Remember reference to the document.
  1552.     fSavedBitMap = fIconDocument->ReturnBitMap()->Copy();// Save a copy of the current bitmap.
  1553. }
  1554.  
  1555.  
  1556. //----------------------------------------------------------------------------------------
  1557. // TIconEditCommand::Free: 
  1558. //----------------------------------------------------------------------------------------
  1559. #pragma segment ADoCommand
  1560.  
  1561. TIconEditCommand::~TIconEditCommand()
  1562. {
  1563.     fSavedBitMap = (TIconBitMap *)FreeIfObject(fSavedBitMap);// Free the saved bitMap
  1564. }
  1565.  
  1566.  
  1567. //----------------------------------------------------------------------------------------
  1568. // TIconEditCommand::DoIt: 
  1569. //----------------------------------------------------------------------------------------
  1570. #pragma segment ADoCommand
  1571.  
  1572. void TIconEditCommand::DoIt()
  1573. {
  1574.     if (fIdentifier != cClear)                    // If the command is Cut or Copy…
  1575.     {
  1576.         TIconDocument * clipDocument;
  1577.         TIconEditView * clipView;
  1578.  
  1579.         // …copy the data to the clipboard.
  1580.  
  1581.         // Need a document to represent the data to be put on the clipboard.
  1582.         // If IIconDocument fails, it should free itself
  1583.         clipDocument = new TIconDocument;
  1584.         clipDocument->IIconDocument(NULL);
  1585.  
  1586.         // If IIconEditView fails it should free itself, but we need to free the document
  1587.         FailInfo fi;
  1588.         Try(fi)
  1589.         {
  1590.             clipView = new TIconEditView;
  1591.             clipView->IIconEditView(clipDocument, NULL, gZeroVPt, fIconEditView->GetMagnification());
  1592.  
  1593.             fSavedBitMap->CopyDataTo(clipDocument->ReturnBitMap());// Copy data to document icon.
  1594.  
  1595.             fi.Success();                        // Don't need failure handler anymore.
  1596.         }
  1597.         else                                    // Recover
  1598.         {
  1599.             clipDocument = (TIconDocument *)FreeIfObject(clipDocument);// Free clipDocument if non-NULL.
  1600.             fi.ReSignal();
  1601.         }
  1602.  
  1603.         // Tell the app we have a new clipboard.
  1604.         ClaimClipboard(clipView);
  1605.     }
  1606.  
  1607.     if (fIdentifier != cCopy)                    // Cut or Clear commands clear the icon.
  1608.         fIconDocument->ClearIcon();                // Clear the icon.
  1609.  
  1610. }
  1611.  
  1612.  
  1613. //----------------------------------------------------------------------------------------
  1614. // TIconEditCommand::RedoIt: 
  1615. //----------------------------------------------------------------------------------------
  1616. #pragma segment ADoCommand
  1617.  
  1618. void TIconEditCommand::RedoIt()
  1619. {
  1620.     if (fIdentifier != cCopy)                    // Cut or Clear commands clear the icon.
  1621.         fIconDocument->ClearIcon();
  1622. }
  1623.  
  1624.  
  1625. //----------------------------------------------------------------------------------------
  1626. // TIconEditCommand::UndoIt: 
  1627. //----------------------------------------------------------------------------------------
  1628. #pragma segment ADoCommand
  1629.  
  1630. void TIconEditCommand::UndoIt()
  1631. {
  1632.     // If the command was Cut or Clear, the new
  1633.     if (fIdentifier != cCopy)                    // …icon was cleared, so restore it.
  1634.     {
  1635.         fSavedBitMap->CopyDataTo(fIconDocument->ReturnBitMap());
  1636.         fIconDocument->RedrawViews();            // Make sure all views get redrawn.
  1637.     }
  1638. }
  1639.  
  1640. //========================================================================================
  1641. // CLASS TIconPasteCommand
  1642. //========================================================================================
  1643. #undef Inherited
  1644. #define Inherited TCommand
  1645.  
  1646. #pragma segment ASelCommand
  1647. MA_DEFINE_CLASS_M1(TIconPasteCommand,
  1648.                    Inherited);
  1649.  
  1650. //----------------------------------------------------------------------------------------
  1651. // TIconPasteCommand constructor 
  1652. //----------------------------------------------------------------------------------------
  1653. #pragma segment ASelCommand
  1654.  
  1655. TIconPasteCommand::TIconPasteCommand() :
  1656.     fIconDocument(NULL),
  1657.     fIconEditView(NULL),
  1658.     fSavedIcon(NULL)
  1659. {
  1660. }
  1661. // TIconPasteCommand::TIconPasteCommand 
  1662.  
  1663. //----------------------------------------------------------------------------------------
  1664. // TIconPasteCommand::IIconPasteCommand: 
  1665. //----------------------------------------------------------------------------------------
  1666. #pragma segment ASelCommand
  1667.  
  1668. void TIconPasteCommand::IIconPasteCommand(TIconEditView* itsIconEditView,
  1669.                                           TIconDocument* itsIconDocument)
  1670. {
  1671.     ICommand(cPaste,                        // Initialize the command…
  1672.                    itsIconDocument,                // Associate it with a document.
  1673.                    kCanUndo, kCausesChange, itsIconDocument);
  1674.  
  1675.     fIconEditView = itsIconEditView;            // Remember reference to the view.
  1676.     fIconDocument = itsIconDocument;            // Remember reference to the document.
  1677.  
  1678.     TIconDocument * clipDoc = (TIconDocument *)gClipboardMgr->fClipView->fDocument;
  1679.     fSavedIcon = clipDoc->ReturnBitMap()->Copy();
  1680. }
  1681.  
  1682. //----------------------------------------------------------------------------------------
  1683. // TIconPasteCommand::Free: 
  1684. //----------------------------------------------------------------------------------------
  1685. #pragma segment ADoCommand
  1686.  
  1687. TIconPasteCommand::~TIconPasteCommand()
  1688. {
  1689.     fSavedIcon = (TIconBitMap *)FreeIfObject(fSavedIcon);
  1690. }
  1691.  
  1692. //----------------------------------------------------------------------------------------
  1693. // TIconPasteCommand::DoIt: 
  1694. //----------------------------------------------------------------------------------------
  1695. #pragma segment ADoCommand
  1696.  
  1697. void TIconPasteCommand::DoIt()
  1698. {
  1699.     TIconBitMap * tempIcon = fIconDocument->ReturnBitMap()->Copy();
  1700.     fIconDocument->SetIcon(fSavedIcon);
  1701.     fSavedIcon = tempIcon;
  1702. }
  1703.  
  1704. //----------------------------------------------------------------------------------------
  1705. // TIconPasteCommand::UndoIt: 
  1706. //----------------------------------------------------------------------------------------
  1707. #pragma segment ADoCommand
  1708.  
  1709. void TIconPasteCommand::UndoIt()
  1710. {
  1711.     DoIt();
  1712. }
  1713.  
  1714. //========================================================================================
  1715. // CLASS TIconBitMap
  1716. //========================================================================================
  1717. #undef Inherited
  1718. #define Inherited TObject
  1719.  
  1720. #pragma segment AOpen
  1721. MA_DEFINE_CLASS_M1(TIconBitMap,
  1722.                    Inherited);
  1723.  
  1724. //----------------------------------------------------------------------------------------
  1725. // TIconBitMap constructor 
  1726. //----------------------------------------------------------------------------------------
  1727. #pragma segment AOpen
  1728.  
  1729. TIconBitMap::TIconBitMap() :
  1730.     fDataHandle(NULL)
  1731. {
  1732. }
  1733. // TIconBitMap::TIconBitMap 
  1734.  
  1735. //----------------------------------------------------------------------------------------
  1736. // TIconBitMap::IIconBitMap: 
  1737. //----------------------------------------------------------------------------------------
  1738. #pragma segment ARes
  1739.  
  1740. void TIconBitMap::IIconBitMap()
  1741. {
  1742.     IObject();
  1743.  
  1744.     FailInfo fi;
  1745.     Try(fi)
  1746.     {
  1747.         fDataHandle = NewPermHandle(kIconSizeInBytes);// Allocate a handle for the
  1748.         // bitmap.
  1749.         fi.Success();
  1750.     }
  1751.     else
  1752.     {
  1753.         Free();
  1754.         fi.ReSignal();
  1755.     }
  1756. }
  1757.  
  1758.  
  1759. //----------------------------------------------------------------------------------------
  1760. // TIconBitMap::Free: 
  1761. //----------------------------------------------------------------------------------------
  1762. #pragma segment ARes
  1763.  
  1764. TIconBitMap::~TIconBitMap()
  1765. {
  1766.     fDataHandle = DisposeIfHandle(fDataHandle);    // dispose of icon data.
  1767. }
  1768.  
  1769. //----------------------------------------------------------------------------------------
  1770. // TIconBitMap::SetIconBitMap: 
  1771. //----------------------------------------------------------------------------------------
  1772. #pragma segment ARes
  1773.  
  1774. void TIconBitMap::SetIconBitMap(Handle theBitMap)
  1775. {
  1776.     MABlockMove(*theBitMap, *fDataHandle,        // …then copy it into the document's
  1777.                 kIconSizeInBytes);                // …icon bitmap.
  1778. }
  1779.  
  1780.  
  1781. //----------------------------------------------------------------------------------------
  1782. // TIconBitMap::ReadFrom: 
  1783. //----------------------------------------------------------------------------------------
  1784. #pragma segment AReadFile
  1785.  
  1786. void TIconBitMap::ReadFrom(TStream* aStream)
  1787. {
  1788.     long numberOfBytes;
  1789.  
  1790.     numberOfBytes = kIconSizeInBytes;
  1791.     aStream->ReadBytes(*fDataHandle, numberOfBytes);
  1792. }
  1793.  
  1794.  
  1795. //----------------------------------------------------------------------------------------
  1796. // TIconBitMap::WriteTo: 
  1797. //----------------------------------------------------------------------------------------
  1798. #pragma segment AWriteFile
  1799.  
  1800. void TIconBitMap::WriteTo(TStream* aStream)
  1801. {
  1802.     long numberOfBytes;
  1803.  
  1804.     numberOfBytes = kIconSizeInBytes;
  1805.     aStream->WriteBytes(*fDataHandle, numberOfBytes);
  1806. }
  1807.  
  1808.  
  1809. //----------------------------------------------------------------------------------------
  1810. // TIconBitMap::LoadFromScrap: 
  1811. //----------------------------------------------------------------------------------------
  1812. #pragma segment AClipboard
  1813.  
  1814. void TIconBitMap::LoadFromScrap()
  1815. {
  1816.     long offset;
  1817.     long err;
  1818.  
  1819.     err = GetScrap(fDataHandle, kIconClipType, &offset);// Load the icon's data from the
  1820.     if (err < 0)
  1821.         FailOSErr((short)err);                    // the clip board and handle errors.
  1822. }
  1823.  
  1824.  
  1825. //----------------------------------------------------------------------------------------
  1826. // TIconBitMap::WriteToScrap: 
  1827. //----------------------------------------------------------------------------------------
  1828. #pragma segment AClipboard
  1829.  
  1830. void TIconBitMap::WriteToScrap()
  1831. {
  1832.     FailOSErr(gClipboardMgr->PutDeskScrapData(kIconClipType,// Write the handle to the desk scrap, //new
  1833.                                               fDataHandle));// …failing if an error occurs.
  1834. }
  1835.  
  1836. //----------------------------------------------------------------------------------------
  1837. // TIconBitMap::Copy: 
  1838. //----------------------------------------------------------------------------------------
  1839. #pragma segment ARes
  1840.  
  1841. TIconBitMap* TIconBitMap::Copy()
  1842. {
  1843.     TIconBitMap * copyOfIcon;
  1844.  
  1845.     copyOfIcon = new TIconBitMap;                // Create a TIconBitMap object.
  1846.     copyOfIcon->IIconBitMap();                    // Initialize it.
  1847.     copyOfIcon->SetIconBitMap(fDataHandle);        // Copy the data.
  1848.     return copyOfIcon;                            // Return a reference to the new handle.
  1849. }
  1850.  
  1851.  
  1852. //----------------------------------------------------------------------------------------
  1853. // TIconBitMap::CopyDataTo: 
  1854. //----------------------------------------------------------------------------------------
  1855. #pragma segment ARes
  1856.  
  1857. void TIconBitMap::CopyDataTo(TIconBitMap* anIcon)
  1858. {
  1859.     anIcon->SetIconBitMap(fDataHandle);            // Copy data to the new icon.
  1860. }
  1861.  
  1862.  
  1863. //----------------------------------------------------------------------------------------
  1864. // TIconBitMap::Clear: 
  1865. //----------------------------------------------------------------------------------------
  1866. #pragma segment ARes
  1867.  
  1868. void TIconBitMap::Clear()
  1869. {
  1870.     long* longPtr;
  1871.  
  1872.     longPtr = (long*) * fDataHandle;
  1873.     for (short i = 0; i < kIconSizeInLongs; i++)
  1874.         *longPtr++ = 0;
  1875. }
  1876.  
  1877.  
  1878. //----------------------------------------------------------------------------------------
  1879. // TIconBitMap::Invert: 
  1880. //----------------------------------------------------------------------------------------
  1881. #pragma segment ARes
  1882.  
  1883. void TIconBitMap::Invert()
  1884. {
  1885.     long* longPtr;
  1886.  
  1887.     longPtr = (long*) * fDataHandle;
  1888.     for (short i = 0; i < kIconSizeInLongs; i++)
  1889.     {
  1890.         *longPtr = ~*longPtr;
  1891.         longPtr++;
  1892.     }
  1893. }
  1894.  
  1895.  
  1896. //----------------------------------------------------------------------------------------
  1897. // TIconBitMap::Draw: 
  1898. //----------------------------------------------------------------------------------------
  1899. #pragma segment ARes
  1900.  
  1901. void TIconBitMap::Draw(const CRect& area,
  1902.                        const CRGBColor& drawingColor)
  1903. {
  1904.     CRGBColor saveColor(GetIfColor());
  1905.     SetIfColor(drawingColor);
  1906.  
  1907.     PenNormal();
  1908.     PlotIcon(area, fDataHandle);
  1909.  
  1910.     SetIfColor(saveColor);
  1911. }
  1912.  
  1913.  
  1914. //----------------------------------------------------------------------------------------
  1915. // TIconBitMap::GetBit: 
  1916. //----------------------------------------------------------------------------------------
  1917. #pragma segment ARes
  1918.  
  1919. Boolean TIconBitMap::GetBit(VPoint iconBit)
  1920. //  Returns the state of the given bit in the icon being drawn. 
  1921. {
  1922.     short byte;
  1923.     short bitInByte;
  1924.  
  1925.     IconBitToByteBit(iconBit, byte, bitInByte);
  1926.  
  1927.     return (*(*fDataHandle + byte) >> bitInByte) & 1;
  1928. }
  1929.  
  1930.  
  1931. //----------------------------------------------------------------------------------------
  1932. // TIconBitMap::SetBit:  Set the state of the given bit in the icon being drawn. 
  1933. //----------------------------------------------------------------------------------------
  1934. #pragma segment ARes
  1935.  
  1936. void TIconBitMap::SetBit(VPoint iconBit,
  1937.                          Boolean turnBitOn)
  1938. {
  1939.     short byte;
  1940.     short bitInByte;
  1941.     char theMask;
  1942.  
  1943.     IconBitToByteBit(iconBit, byte, bitInByte);
  1944.  
  1945.     theMask = (1 << bitInByte);
  1946.  
  1947.     if (turnBitOn)
  1948.         *(*fDataHandle + byte) |= theMask;
  1949.     else
  1950.         *(*fDataHandle + byte) &= ~theMask;        // AND the complement of the mask.
  1951. }
  1952.  
  1953.  
  1954. //----------------------------------------------------------------------------------------
  1955. // TIconBitMap::IconBitToByteBit: This converts the given icon bit to a byte and bit in an
  1956. // array of long words.
  1957. //----------------------------------------------------------------------------------------
  1958. #pragma segment ARes
  1959.  
  1960. void TIconBitMap::IconBitToByteBit(VPoint iconBit,
  1961.                                    short& byte,
  1962.                                    short& bit)
  1963. {
  1964.     long bitNumber;
  1965.  
  1966.     bitNumber = iconBit.v * kIconVBits + iconBit.h;
  1967.     byte = (short)bitNumber / 8;
  1968.     bit = (short)(7 - (bitNumber % 8));
  1969. }
  1970.  
  1971.  
  1972. //========================================================================================
  1973. // CLASS TZoomOutCommand
  1974. //========================================================================================
  1975. #undef Inherited
  1976. #define Inherited TCommand
  1977.  
  1978. #pragma segment ASelCommand
  1979. MA_DEFINE_CLASS_M1(TZoomOutCommand,
  1980.                    Inherited);
  1981.  
  1982. //----------------------------------------------------------------------------------------
  1983. // TZoomOutCommand constructor 
  1984. //----------------------------------------------------------------------------------------
  1985. #pragma segment ASelCommand
  1986.  
  1987. TZoomOutCommand::TZoomOutCommand() :
  1988.     fIconDocument(NULL)
  1989. {
  1990. }
  1991. // TZoomOutCommand::TZoomOutCommand()  
  1992.  
  1993. //----------------------------------------------------------------------------------------
  1994. // TZoomOutCommand destructor
  1995. //----------------------------------------------------------------------------------------
  1996. #pragma segment MADestructorRes
  1997.  
  1998. TZoomOutCommand::~TZoomOutCommand()
  1999. {
  2000. }
  2001.  
  2002. //----------------------------------------------------------------------------------------
  2003. // TZoomOutCommand::IZoomOutCommand
  2004. //----------------------------------------------------------------------------------------
  2005. #pragma segment ASelCommand
  2006.  
  2007. void TZoomOutCommand::IZoomOutCommand(TIconDocument* itsIconDocument)
  2008. {
  2009.     fIconDocument = itsIconDocument;            // Save a reference to the icon document.
  2010.  
  2011.     ICommand(cZoomOut,                    // Initialize the inherited data…
  2012.                    itsIconDocument, kCanUndo, kCausesChange, itsIconDocument);// …no view or scroller to track.
  2013. }
  2014.  
  2015. //----------------------------------------------------------------------------------------
  2016. // TZoomOutCommand::MakeAppleEvent() 
  2017. //----------------------------------------------------------------------------------------
  2018. TAppleEvent* TZoomOutCommand::MakeAppleEvent()
  2019. {
  2020.     CTempDesc directObjectDesc;
  2021.     TAppleEvent * theZoomOutAppleEvent = new TAppleEvent;
  2022.  
  2023.     theZoomOutAppleEvent->IAppleEvent(kAEIconEditClass, kAEZoomOutID, gServerAddress, kAENoReply);
  2024.     fIconDocument->MakeObjectSpecifier(directObjectDesc, fIconDocument->GetSpecifierForm());
  2025.     theZoomOutAppleEvent->WriteParameter(keyDirectObject, directObjectDesc);
  2026.  
  2027.     return theZoomOutAppleEvent;
  2028. }
  2029.  
  2030. //----------------------------------------------------------------------------------------
  2031. // TZoomOutCommand::DoIt: 
  2032. //----------------------------------------------------------------------------------------
  2033. #pragma segment ADoCommand
  2034.  
  2035. void TZoomOutCommand::DoIt()
  2036. {
  2037.     short currentMagnification = fIconDocument->GetIconView()->GetMagnification();
  2038.     fIconDocument->GetIconView()->SetMagnification(currentMagnification - 2);
  2039. }
  2040.  
  2041.  
  2042. //----------------------------------------------------------------------------------------
  2043. // TZoomOutCommand::UndoIt: 
  2044. //----------------------------------------------------------------------------------------
  2045. #pragma segment ADoCommand
  2046.  
  2047. void TZoomOutCommand::UndoIt()
  2048. {
  2049.     short currentMagnification = fIconDocument->GetIconView()->GetMagnification();
  2050.     fIconDocument->GetIconView()->SetMagnification(currentMagnification + 2);
  2051. }
  2052.  
  2053.  
  2054. //----------------------------------------------------------------------------------------
  2055. // TZoomOutCommand::RedoIt: 
  2056. //----------------------------------------------------------------------------------------
  2057. #pragma segment ADoCommand
  2058.  
  2059. void TZoomOutCommand::RedoIt()
  2060. {
  2061.     short currentMagnification = fIconDocument->GetIconView()->GetMagnification();
  2062.     fIconDocument->GetIconView()->SetMagnification(currentMagnification - 2);
  2063. }
  2064.  
  2065. //========================================================================================
  2066. // CLASS TZoomInCommand
  2067. //========================================================================================
  2068. #undef Inherited
  2069. #define Inherited TCommand
  2070.  
  2071. #pragma segment ASelCommand
  2072. MA_DEFINE_CLASS_M1(TZoomInCommand,
  2073.                    Inherited);
  2074.  
  2075. //----------------------------------------------------------------------------------------
  2076. // TZoomInCommand constructor 
  2077. //----------------------------------------------------------------------------------------
  2078. #pragma segment ASelCommand
  2079.  
  2080. TZoomInCommand::TZoomInCommand() :
  2081.     fIconDocument(NULL)
  2082. {
  2083. }
  2084. // TZoomInCommand::TZoomInCommand()  
  2085.  
  2086. //----------------------------------------------------------------------------------------
  2087. // TZoomInCommand destructor
  2088. //----------------------------------------------------------------------------------------
  2089. #pragma segment MADestructorRes
  2090.  
  2091. TZoomInCommand::~TZoomInCommand()
  2092. {
  2093. }
  2094.  
  2095. //----------------------------------------------------------------------------------------
  2096. // TZoomInCommand::IZoomInCommand
  2097. //----------------------------------------------------------------------------------------
  2098. #pragma segment ASelCommand
  2099.  
  2100. void TZoomInCommand::IZoomInCommand(TIconDocument* itsIconDocument)
  2101. {
  2102.     fIconDocument = itsIconDocument;            // Save a reference to the icon document.
  2103.  
  2104.     ICommand(cZoomIn,                        // Initialize the inherited data…
  2105.                    itsIconDocument, kCanUndo, kCausesChange, itsIconDocument);// …no view or scroller to track.
  2106. }
  2107.  
  2108. //----------------------------------------------------------------------------------------
  2109. // TZoomInCommand::MakeAppleEvent() 
  2110. //----------------------------------------------------------------------------------------
  2111. TAppleEvent* TZoomInCommand::MakeAppleEvent()
  2112. {
  2113.     CTempDesc directObjectDesc;
  2114.     TAppleEvent * theZoomInAppleEvent = new TAppleEvent;
  2115.  
  2116.     theZoomInAppleEvent->IAppleEvent(kAEIconEditClass, kAEZoomInID, gServerAddress, kAENoReply);
  2117.     fIconDocument->MakeObjectSpecifier(directObjectDesc, fIconDocument->GetSpecifierForm());
  2118.     theZoomInAppleEvent->WriteParameter(keyDirectObject, directObjectDesc);
  2119.  
  2120.     return theZoomInAppleEvent;
  2121. }
  2122.  
  2123. //----------------------------------------------------------------------------------------
  2124. // TZoomInCommand::DoIt: 
  2125. //----------------------------------------------------------------------------------------
  2126. #pragma segment ADoCommand
  2127.  
  2128. void TZoomInCommand::DoIt()
  2129. {
  2130.     short currentMagnification = fIconDocument->GetIconView()->GetMagnification();
  2131.     fIconDocument->GetIconView()->SetMagnification(currentMagnification + 2);
  2132. }
  2133.  
  2134.  
  2135. //----------------------------------------------------------------------------------------
  2136. // TZoomInCommand::UndoIt: 
  2137. //----------------------------------------------------------------------------------------
  2138. #pragma segment ADoCommand
  2139.  
  2140. void TZoomInCommand::UndoIt()
  2141. {
  2142.     short currentMagnification = fIconDocument->GetIconView()->GetMagnification();
  2143.     fIconDocument->GetIconView()->SetMagnification(currentMagnification - 2);
  2144. }
  2145.  
  2146.  
  2147. //----------------------------------------------------------------------------------------
  2148. // TZoomInCommand::RedoIt: 
  2149. //----------------------------------------------------------------------------------------
  2150. #pragma segment ADoCommand
  2151.  
  2152. void TZoomInCommand::RedoIt()
  2153. {
  2154.     short currentMagnification = fIconDocument->GetIconView()->GetMagnification();
  2155.     fIconDocument->GetIconView()->SetMagnification(currentMagnification + 2);
  2156. }
  2157.  
  2158. //----------------------------------------------------------------------------------------
  2159. // End of UIconEdit.cp
  2160.  
  2161. #pragma segment Inline
  2162.  
  2163.